/**
 * C Implementation: grib_multi
 *
 * Description:
 *  How to decode GRIB messages containing multiple fields.
 *  Try to turn on and off multi support to see the difference. Default is OFF.
 *  For all the tools default is multi support ON.
 * 
 * 如何解码包含多个的 GRIB 消息
 * 尝试打开和关闭多支持以查看差异。 默认为关闭。
 * 对于所有工具，默认为多支持开启。
 *
 */
#include <stdio.h>

#include "eccodes.h"

int sfx_grib_multi()
{
    int err                = 0;
    long parameterCategory = 0; // 参数类别
    long parameterNumber   = 0; // 参数编号
    long discipline        = 0; // 学科

    char* filename = "d:/sfxData/ECMWF/eccodes_test_data/multi.grib2";

    // turn on support for multi fields messages (打开对多字段消息的支持)
    codes_grib_multi_support_on(NULL);
    // codes_grib_multi_support_off(NULL);

    FILE* in = fopen(filename, "rb");

    codes_handle* h = NULL;
    while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_GRIB, &err)) != NULL) {
        //
        CODES_CHECK(err, NULL);

        CODES_CHECK(codes_get_long(h, "discipline", &discipline), 0);
        printf("discipline = %ld\n", discipline);

        CODES_CHECK(codes_get_long(h, "parameterCategory", &parameterCategory), 0);
        printf("parameterCategory=%ld\n", parameterCategory);

        CODES_CHECK(codes_get_long(h, "parameterNumber", &parameterNumber), 0);
        printf("parameterNumber=%ld\n", parameterNumber);

        if (discipline == 0 && parameterCategory == 2) {
            if (parameterNumber == 2) printf("-------- u -------\n");
            if (parameterNumber == 3) printf("-------- v -------\n");
        }

        codes_handle_delete(h);
    }

    fclose(in);
    return 0;
}
